package com.opoc.controller;

import com.opoc.config.BaseContext;
import com.opoc.mapper.PoolMapper;
import com.opoc.pojo.Pool;
import com.opoc.pojo.PoolBasic;
import com.opoc.pojo.PoolTest;
import com.opoc.pojo.Result;
import com.opoc.service.PoolService;
import com.opoc.service.SystemLogService;
import com.opoc.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/pool")
@Slf4j
public class PoolController {

  @Autowired
  PoolService poolService;
  @Autowired
  SystemLogService systemLogService;
  @Autowired
  PoolMapper poolMapper;

  /**
   * 获取池子基本信息
   */
  @GetMapping("/basic")
  public Result getPoolBasicInfo(
          @RequestParam(value = "company_name", required = false) String company_name,
          @RequestParam(value = "pool_id", required = false) String pool_id,
          @RequestParam(value = "pool_name", required = false) String pool_name
  ) {
    log.info(company_name, pool_id, pool_name);
    List<PoolBasic> data = poolService.getPoolBasicInfo(company_name, pool_id, pool_name);
    return data.isEmpty() ? Result.error("无数据") : Result.success(data);
  }

  /**
   * 池子检测信息查询
   */
  @GetMapping("/check")
  public Result getPoolDetectionInfo(
          @RequestParam(value = "company_name", required = false) String company_name,
          @RequestParam(value = "pool_id", required = false) String pool_id,
          @RequestParam(value = "pool_name", required = false) String pool_name
  ) {
    List<PoolTest> poolList = poolService.getPoolDetectionInfo(company_name, pool_id, pool_name);
    return poolList.isEmpty() ? Result.error("无数据") : Result.success(poolList) ;
  }

  /**
   *添加池子
   */
  @PostMapping("/add")
  public Result addPool(@RequestBody Pool pool) throws IOException, ParseException {
    String err = poolService.addPool(pool);
    systemLogService.addLog("池子管理", "添加", BaseContext.getCurrentId(), err == null? "成功" : "失败");
    return err == null ? Result.success() : Result.error(err);
  }

  /**
   * 批量导入池子
   */
  @PostMapping("/import")
  public Result importPool(@RequestParam("import_file") MultipartFile file) throws Exception {
    String fileExtension = FileUtils.getFileExtension(file);
    if (FileUtils.EMPTY.equals(fileExtension)) {
      return Result.error("仅支持xls和xlsx格式文件");
    } else { //格式正确 提取文件数据
      String err = poolService.importPool(file);
      systemLogService.addLog("池子管理", "导入", BaseContext.getCurrentId(), err == null? "成功" : "失败");
      return err == null ? Result.success() : Result.error(err);
    }
  }

  /**
   *批量导出池子
   */
  @GetMapping("/export")
  public Result exportPool(@RequestParam("pool_ids") List<String> ids) throws IOException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, ParseException {
    if (ids.isEmpty()){
      return Result.error("未选择池子");
    }
    String file_url = poolService.exportPool(ids);
    systemLogService.addLog("池子管理", "导出", BaseContext.getCurrentId(), file_url != null? "成功" : "失败");
    return file_url == null ? Result.error("导出失败") : Result.success(file_url);
  }

  /**
   * 批量删除池子
   */
  @DeleteMapping("/delete")
  public Result deleteByIds(@RequestParam("pool_ids") List<String> pool_ids) throws ParseException {
    if (pool_ids.isEmpty()) {
      log.info(pool_ids.toString());
      return Result.error("is_null");
    }
    log.info(pool_ids.toString());
    String err = poolService.delete(pool_ids);
    systemLogService.addLog("池子管理", "删除", BaseContext.getCurrentId(), err == null ? "成功" : "失败");
    return err != null ? Result.error(err) : Result.success();
  }
  /**
   * 图片上传
   */
  @PostMapping("/upload")
  public Result uploadImage(@RequestParam(value = "sewage_treatment_flow_chart", required = false) MultipartFile sewage_treatment_flow_chart,
                            @RequestParam(value = "pool_code", required = false) MultipartFile pool_code,
                            @RequestParam("pool_id") String pool_id) {
    if (!poolMapper.validatePoolById(pool_id)){
      return Result.error("池子不存在");
    }
    if (sewage_treatment_flow_chart == null && pool_code == null){
      return Result.error("未选择上传文件");
    }

    Map<String, String> result = poolService.uploadImage(sewage_treatment_flow_chart, pool_code, pool_id);

    return result.isEmpty() ? Result.error("上传失败") : Result.success(result);
  }

}

